package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        String queryString = queryPageBean.getQueryString();
        Page<Role> page = roleDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(Integer[] permissionIds, Integer[] menuIds, Role role) {
        //添加角色基本信息
        roleDao.add(role);
        //添加角色-权限关系
        Integer roleId = role.getId();
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("permission_id", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
        //添加角色-菜单关系
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("menu_id", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }

    @Override
    public void delete(Integer id) {

        Integer permissionCount = roleDao.findAssociationWithPer(id);
        Integer menuCount = roleDao.findAssociationWithMenu(id);
        if (permissionCount > 0 && menuCount > 0) {
            new RuntimeException();
        }
        roleDao.delete(id);
    }

    @Override
    public Role findById(Integer id) {
        Role role = roleDao.findById(id);
        return role;
    }

    @Override
    public void edit(Integer[] permissionIds, Integer[] menuIds, Role role) {
        //修改角色基本信息
        roleDao.edit(role);
        Integer roleId = role.getId();
        //删除原有表关系
        roleDao.deleteAssociationWithPer(roleId);
        roleDao.deleteAssociationWithMenu(roleId);
        //建立新表关系
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("permission_id", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
        //添加角色-菜单关系
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map map = new HashMap<>();
                map.put("role_id", roleId);
                map.put("menu_id", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }
}
